<template>
  <div class="downloadTemplate">
    <!-- 按钮组件导入、导出、下载模板 -->
    <el-button icon="el-icon-download" type="success" plain @click="downloadTemplate" size="mini">{{nbDownTitle?'换表模板':'下载模板'}}</el-button>
  </div>
</template>

<script>
export default {
  name: 'ImportXlsx',
  props: {
    nbDownTitle: {
      default: '',
      type: String,
    },
  },
  //   props:['suoS', 'multipleSelection','importUrl','disabledImport','disabled'],
  data() {
    return {
      disableExportData: false,
    }
  },
  methods: {
    // 弹窗关闭
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(() => {
          done()
        })
        .catch((_) => {})
    },
    // 模板下载
    async downloadTemplate() {
      console.log('模板下载', this.$parent.downloadFile, 6626)
      try {
        const { downloadFile } = this.$parent
        const res = await downloadFile()
        console.log(2222)

        // const res = await this.$parent.downloadFile()

        // let iconv = require('iconv-lite');
        // iconv.skipDecodeWarning = true;//忽略警告
        // let fileName = iconv.decode(res.filename, 'utf-8');
        // let fenge =fileName.split(';')[1]
        // fileName = fenge.split('=')[1]
        console.log(res, '导出结果')
        let fileName = decodeURI(res['filename'].split('=')[1])
        // const blob  = new Blob([res],{type: 'application/vnd.ms-excel;charset=utf-8'})
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
        var downloadA = document.createElement('a')
        downloadA.download = fileName
        downloadA.style.display = 'none'
        downloadA.href = URL.createObjectURL(blob)
        document.body.appendChild(downloadA)
        downloadA.click()
        URL.revokeObjectURL(downloadA.href)
        document.body.removeChild(downloadA)
        // 提示
        this.$notify({
          title: '下载模板成功',
          message: fileName,
          type: 'success',
        })
      } catch (error) {
        console.log(error)
        this.$notify.error({
          title: '错误',
          message: '下载失败！',
        })
      }
    },
  },
  created() {
    // this.disabledExport()
  },
  wacth: {
    // disabledImport(newValue){
    //   this.disabledImport = newValue
    // }
  },
  // watch:{
  //   disabledExport(newValue){
  //     this.disabledExport = newValue
  //     this.disabledExport()
  //   }
  // }
}
</script>

<style lang="scss" scoped>
.ImportXlsx {
  display: inline-block;
}
</style>
